<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"])) {
	if ($_REQUEST["reqtype"] == "save"){
		$errMsg .= menuPageIns();
	}
	$module = sxREQUEST("module");
	$menuPage = sxREQUEST("menu_page");
	$menuGroup = sxREQUEST("menu_group");
	$menuLine = sxREQUEST("menu_line");
	$sequence = sxREQUEST("sequence");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
	$newGroup = sxREQUEST("new_group");
	$newGFlag = sxREQUEST("new_gflag");
	$newGSeq = sxREQUEST("new_gseq");
	$processUnit = sxREQUEST("process_unit");
	$linkedPage = sxREQUEST("linked_page");
	$linkedModule = sxREQUEST("linked_module");
	$queryString = sxREQUEST("query_string");
}else{
	if(!isset($_REQUEST["mod"])){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("variabile 'mod' non passata")); 
		serverRedir($url);
	}
	if(!isset($_REQUEST["menu_page"])){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("variabile 'menu_page' non passata")); 
		serverRedir($url);
	}
	$linkedModule = $module = sxREQ('mod');
	$menuPage = sxREQ('menu_page');
	$menuGroup = '';
	$menuLine = '';
	$sequence = 100;
	$description = "";
	$note = "";
	$newGroup = "";
	$newGFlag = "N";
	$newGSeq = "10";
	$linkedPage = "";
	$processUnit = "";
	$queryString = "";
}
$sql = "SELECT m.description AS mdes, mp.description AS pdes " .
		" FROM menu_page mp " .
		" INNER JOIN module m ON m.module = mp.module ".
		" WHERE mp.module = '$module' AND mp.menu_page = '$menuPage'";
$objSDAO->query($sql,"forvis");
$recvis = $objSDAO->fetch("forvis");
//output HTML
$aOpz = array("tit2"=>_t("Crereqtype nuova voce men@"), "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		document.objFrm.reqtype.value = 'save';
		document.objFrm.submit();
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"menuPageLineIns.php\" method=\"POST\" enctype=\"multipart/form-data\">\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"reload\">\n");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">\n");
echo("<input type=\"hidden\" name=\"module\" id=\"module\" value=\"$module\">\n");
echo("<input type=\"hidden\" name=\"menu_page\" id=\"menu_page\" value=\"$menuPage\">\n");
//output comandi
$aCmd = array("insMP"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("conferma inserimento nuova pagina men@"),
										"text"=>_t("conferma")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>"cancel inserimento",
										"text"=>"cancel"
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='8'>"._t("Modulo").": $module - ".$recvis['mdes']."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("pagina")."</td>" .
			"<td class=\"text01\" colspan='7'>".$menuPage.' - '.$recvis['pdes']."</td>" .
		"</tr>\n");
// ----------------------------------------------------------------- gruppo		
echo("<tr>" .
			"<td class=\"labl01\" width='15%'>"._t("gruppo voci")."</td>" .
			"<td class=\"text01\" width='30%'>" .
			"<select class=\"inpt01\" name=\"menu_group\" id=\"linked_page\" onChange='document.objFrm.submit();'>\n");
echo("<option value='' ".(($menuGroup=='')?'SELECTED':'')." >-- nessun gruppo</option>\n");
$sql = "SELECT * FROM menu_group WHERE module='$module' AND menu_page = '$menuPage' ORDER BY menu_group";
$objSDAO->query($sql,"select");
while($rowsp = $objSDAO->fetch("select")) {
	echo("<option value=\"".$rowsp['menu_group']."\" ".
	(($menuGroup==$rowsp['menu_group'])?'SELECTED':'').">".
	$rowsp['menu_group']." - ".$rowsp['description']."</option>\n");
}
echo("<option value='+crea' ".(($menuGroup=='+crea')?'SELECTED':'').">-- crea gruppo</option>\n");
echo("</select>\n");
echo("</td>\n");
if ($menuGroup=='+crea') {
	echo("<td class=\"labl01\" width='15%'>"._t("nuovo gruppo")."</td>\n" .
			"<td class=\"text01\" width='5%'>\n" .
			"<input class=\"inpt01\" type=\"text\" name=\"new_group\" id=\"new_group\" maxlength=\"4\" value=\"$newGroup\" size='4'>" .
			"</td>\n" .
			"<td class=\"labl01\" width='12%'>"._t("seq. gruppo")."</td>\n" .
			"<td class=\"text01\" width='5%'>" .
			"<input class=\"inpt01\" type=\"text\" name=\"new_gseq\" id=\"new_gseq\" maxlength=\"4\" value=\"$newGSeq\" size='4'>" .
			"</td>\n" .
			"<td class=\"labl01\" width='13%'>"._t("visualizza gruppo")."</td>\n" .
			"<td class=\"text01\" width='5%'>" .
			"<input class=\"text01\" type=\"radio\" name=\"new_gflag\" id=\"new_gflag\" value=\"Y\" " .
			(($newGFlag=='Y')?'CHECKED':'').">"._t('yes') .
			"<input class=\"text01\" type=\"radio\" name=\"new_gflag\" id=\"new_gflag\" value=\"N\" " .
			(($newGFlag!='Y')?'CHECKED':'').">"._t('no') .
			"</td>\n");
} else {
	echo("<td class=\"text01\" width='55%' colspan='6'>&nbsp;</td>\n");
	echo "<input name='new_group' type=hidden value=''>\n";
	echo "<input name='new_gseq' type=hidden value=''>\n";
	echo "<input name='new_gflag' type=hidden value='N'>\n";
}
			
echo("</tr>\n");
// ----------------------------------------------------------------- voce		
echo("<tr>" .
			"<td class=\"labl01\" width='15%'>"._t("voce")." (*)</td>" .
			"<td class=\"text01\" width='30%'>" .
			"<input class=\"inpt01\" type=\"text\" name=\"menu_line\" id=\"menu_line\" maxlength=\"4\" value=\"$menuLine\" size='4'>" .
			"</td>" .
			"<td class=\"labl01\" width='15%'>"._t("sequenza")."</td>" .
			"<td class=\"text01\" colspan='5' width='40%'>" .
			"<input class=\"inpt01\" type=\"text\" name=\"sequence\" id=\"sequence\" maxlength=\"4\" value=\"$sequence\" size='4'>" .
			"</td>" .
		"</tr>\n");
echo("<tr>\n" .
			"<td class=\"labl01\">"._t("description")." (*)</td>" .
			"<td class=\"text01\" colspan='7'><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
// ----------------------------------------------------------------- linked_module		
echo("<tr>\n" .
		"<td class=\"labl01\">"._t("modulo")."</td>\n" .
		"<td class=\"text01\" colspan='7'>\n");
$sql = "SELECT * FROM module m ORDER BY m.module";
$objSDAO->query($sql,"select");
echo("<select class=\"inpt01\" name=\"linked_module\" id=\"linked_module\" onChange='document.objFrm.submit();'>\n");
while($rowsp = $objSDAO->fetch("select")) {
	echo("<option value=\"".$rowsp['module']."\" ".
	(($linkedModule==$rowsp['module'])?'SELECTED':'').">".
	$rowsp['module']." - ".$rowsp['description']."</option>\n");
}
echo("</select>\n");
echo("</td>\n" .
		"</tr>\n");
// ----------------------------------------------------------------- menu_page		
echo("<tr>\n" .
			"<td class=\"labl01\">"._t("pagina menu")."</td>\n" .
			"<td class=\"text01\" colspan='7'>" .
			"<select class=\"inpt01\" name=\"linked_page\" id=\"linked_page\" onChange='document.objFrm.submit();'>\n");
echo("<option value='' ".(($linkedPage=='')?'SELECTED':'').">".
	_t("-- seleziona una pagina di men@ o un processo")."</option>\n");
if ($processUnit=='') { 
	$sql = "SELECT mp.* FROM menu_page mp " .
			" WHERE mp.module='$linkedModule' " .
			(($linkedModule == $module)?" AND mp.menu_page <> '$menuPage' ":"") .
			" ORDER BY mp.menu_page";
	$objSDAO->query($sql,"select");
	while($rowsp = $objSDAO->fetch("select")) {
		echo("<option value=\"".$rowsp['menu_page']."\" ".
		(($linkedPage==$rowsp['menu_page'])?'SELECTED':'').">".
		$rowsp['menu_page']." - ".$rowsp['description']."</option>\n");
	}
}
echo("</select>\n");
echo("</td>\n" .
		"</tr>\n");
// ----------------------------------------------------------------- process		
echo("<tr>" .
		"<td class=\"labl01\">"._t("processo")."</td>" .
		"<td class=\"text01\" colspan='7'>" .
		"<select class=\"inpt01\" name=\"process_unit\" id=\"process_unit\" onChange='document.objFrm.submit();'>\n");
echo("<option value='' ".(($processUnit=='')?'SELECTED':'').">".
	_t("-- seleziona un processo o una pagina di men@")."</option>\n");
if ($linkedPage=='') { 
	$sql = "SELECT * FROM process_unit WHERE module='$linkedModule' ORDER BY process_unit";
	$objSDAO->query($sql,"select");
	while($rowsp = $objSDAO->fetch("select")) {
		echo("<option value=\"".$rowsp['process_unit']."\" ".
		(($processUnit==$rowsp['process_unit'])?'SELECTED':'').">".
		$rowsp['process_unit']."</option>\n");
	}
}
echo("</select>\n");
echo("</td>" .
		"</tr>\n");
// ----------------------------------------------------------------- query string		
echo("<tr>" .
			"<td class=\"labl01\">"._t("query string")."</td>" .
			"<td class=\"text01\" colspan='7'><input class=\"inpt01\" type=\"text\" name=\"query_string\" id=\"query_string\" maxlength=\"250\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
// ----------------------------------------------------------------- note		
echo("<tr>" .
			"<td class=\"labl01\">"._t("note")."</td>" .
			"<td class=\"text01\" colspan='7'><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">$note</textarea></td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));
echo("<div id=\"messArea\" class=\"mess01\">$errMsg</div>");
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());

function menuPageIns(){
	global $objSDAO;
	$errMsg = "";
	$_REQUEST["menu_line"] = strtolower($_REQUEST["menu_line"]);
	if(!ereg("[a-z0-9]{4}",$_REQUEST["menu_line"]))
		$errMsg .= _t("La voce di men@ deve essere un alfanumerico di 4 caratteri").".<br>";
	if(ereg("[^0-9]",$_REQUEST["sequence"]))
		$errMsg .= _t("campo sequenza non numerico").".<br>";
	if(trim($_REQUEST["description"]) == '')
		$errMsg .= _t("La description &egrave; obbligatoria.")."<br>";
	if (trim(sxREQUEST('linked_page')) != '' and trim(sxREQUEST('process_unit'))!= '') {
		$errMsg .= _t("Selezionare una pagina men@ o un processo (non entrambi)").".<br>";
	}
	if ((trim(sxREQUEST('new_group')) != '' or trim(sxREQUEST('new_seq')) != '') 
	and sxREQUEST('menu_group')!= '+crea') {
		$errMsg .= _t("Impostati dati nuovo gruppo senza aver selezionato 'crea gruppo'").".<br>";
	}
	if (sxREQUEST('menu_group')== '+crea') {
		$_REQUEST["new_group"] = strtolower($_REQUEST["new_group"]);
		if(!ereg("[a-z0-9]{4}",$_REQUEST["new_group"]))
			$errMsg .= _t("Il nuovo gruppo deve essere un alfanumerico di 4 caratteri").".<br>";
		if(ereg("[^0-9]",$_REQUEST["new_gseq"]))
			$errMsg .= _t("campo sequenza nuovo gruppo non numerico").".<br>";
		if($errMsg != ""){ return $errMsg; }
		//controllo che il modulo non esista gi@
		$sSQL = "SELECT count(*) as num FROM menu_group " .
				" WHERE module='".sxRAS("module")."' " .
						" AND menu_page = '".sxRAS("menu_page")."'".
						" AND menu_group = '".sxRAS("new_group")."'";
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if($row["num"] != 0) {
			$errMsg .= _t("Nuovo gruppo voci men@ gi@ esistente").".<br>";
		}
		if($errMsg != ""){ return $errMsg; }
		$sSQL = "INSERT INTO menu_group " .
				"(module, " .
				"menu_page, " .
				"menu_group, " .
				"description, " .
				"sequence, " .
				"flag_display, " .
				"note)" .
				"VALUES('".sxRAS("module")."', " .
				"'".sxRAS("menu_page")."', " .
				"'".sxRAS("new_group")."', " .
				"'".sxRAS("new_group")."', " .
				"".intval(sxRAS("new_gseq")).", " .
				"'".sxRAS("new_gflag")."', " .
				"''" .
				")";			
		$objSDAO->query($sSQL,"ins");
		$_REQUEST['menu_group'] = $_REQUEST['new_group'];
	}
	//controllo che la voce non esista gi@
	$sSQL = "SELECT count(*) as num FROM menu_line " .
			" WHERE  menu_line = '".sxRAS("module").".".sxRAS("menu_page").".".sxRAS("menu_line")."'";
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["num"] != 0)
		$errMsg .= _t("La voce di men@ esiste gi&agrave;.<br>");
	if($errMsg != ""){ return $errMsg; }
	$sSQL = "INSERT INTO menu_line " .
			"(module, " .
			"menu_page, " .
			"menu_group, " .
			"menu_line, " .
			"description, " .
			"sequence, " .
			"linked_module, " .
			"linked_page, " .
			"process_unit, " .
			"query_string, " .
			"note)" .
			"VALUES('".sxRAS("module")."', " .
			"'".sxRAS("menu_page")."', " .
			"'".sxRAS("menu_group")."', " .
			"'".sxRAS("module").".".sxRAS("menu_page").".".sxRAS("menu_line")."', " .
			"'".sxRAS("description")."', " .
			"".intval(sxRAS("sequence")).", " .
			"'".sxRAS("linked_module")."', " .
			"'".sxRAS("linked_page")."', " .
			"'".sxRAS("process_unit")."', " .
			"'".sxRAS("query_string")."', " .
			"'".sxRAS("note")."'" .
			")";	
	
	$objSDAO->query($sSQL,"ins");
	logRecord("300", _t("Inserito nuova vove men@ '".sxREQUEST('module').".".sxREQUEST('menu_page').".".sxREQUEST('menu_line')."'"));
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	exit;
}

?>
